package written.test;

import java.util.Arrays;

/**
 * @Author: Dang Qi
 * @Date: 2021/3/28  19:57
 * @Description:
 */
public class Main5 {
    public static void main(String[] args) {

    }
    public static int find(int[] nums){
        int ans = 0;
        int n = nums.length;
        Arrays.sort(nums);

        for(int i = 0; i < n-2; i++){
            for(int j = i+1; j < n - 1; j++){
                int left = leftBound(nums,nums[i]- nums[j], nums[i]+nums[j], j+1);
                int right = right_bound(nums,nums[i]- nums[j], nums[i]+nums[j], j+1);
                if(left==-1) break;
                for(int k = left; k <=right;k++){
                    ans = Math.max(ans, zhiShu(nums[i],nums[j],nums[k]));
                }
            }
        }
        return 0;
    };
    public static int leftBound(int[] nums, int s, int e, int index){
        if(index >= nums.length) return -1;
        int left = index, right = nums.length;
        while (left < right){
            int mid = (left+right)/2;
            if(nums[mid]>s && nums[mid] < e){
                right = mid;
            }else if(nums[mid] <= s)
                left = mid + 1;
            else right = mid;
        }
        return left;
    }
    public static int right_bound(int[] nums, int s, int e, int index) {
        if(index >= nums.length) return -1;
        int left = index, right = nums.length;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid]>s && nums[mid] < e) {
                left = mid + 1; // 注意
            } else if (nums[mid] <= s) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left - 1; // 注意
    }
    public static boolean dengyao(int a, int b, int c){
        return Math.abs(a-b) == Math.abs(a-c) ||
                Math.abs(a-c)==Math.abs(b-c) ||
                Math.abs(a-b)==Math.abs(b-c);
    }
    public static int zhiShu(int a, int b, int c){
        if(dengyao(a,b,c)) return 0;
        return Math.abs(a-b) + Math.abs(a-c)+Math.abs(b-c);
    }
}
